package org.gary.core.action;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.gary.core.service.InitService;
import org.gary.equipment.model.Equipment;
import org.gary.equipment.service.EquipmentService;
import org.gary.product.model.Product;
import org.gary.product.service.ProductService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
/**
 * 核心Action
 * @author Gary
 *
 */
@Component
@Scope("prototype")
public class CoerAction extends ActionSupport implements ServletRequestAware,SessionAware {

	private static final long serialVersionUID = -1337522582258838560L;
	private HttpServletRequest request;
	private ProductService productService;
	private EquipmentService equipmentService;
	public void setEquipmentService(EquipmentService equipmentService) {
		this.equipmentService = equipmentService;
	}

	private InitService initService;
	
	@Resource
	public void setInitService(InitService initService) {
		this.initService = initService;
	}

	@Resource
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	private Map<String, Object> session;
	
	public void setLanguage(){
		String language=request.getParameter("Language");
		
		if("zh_CN".equalsIgnoreCase(language)){
			session.put("WW_TRANS_I18N_LOCALE", Locale.SIMPLIFIED_CHINESE);
		}else if("zh_TW".equalsIgnoreCase(language)){
			session.put("WW_TRANS_I18N_LOCALE", Locale.TRADITIONAL_CHINESE);
		}else if("en_US".equalsIgnoreCase(language)){
			session.put("WW_TRANS_I18N_LOCALE", Locale.US);
		}else{
			session.put("WW_TRANS_I18N_LOCALE", Locale.SIMPLIFIED_CHINESE);
		}
		System.out.println(session.get("WW_TRANS_I18N_LOCALE"));
		initService.initForceAll(session);
	}
	
	public String coer(){
		initService.initAll(session);
		product();	
		equipment();
		return SUCCESS;
	}
	
	private void product(){		
		List<Product> minProduct=productService.getProductList(4,0,session.get("WW_TRANS_I18N_LOCALE"),-1);
		request.setAttribute("minProduct", minProduct);		
	}
	
	private void equipment(){		
		List<Equipment> minEquipment=equipmentService.getEquipmentList(4,0,session.get("WW_TRANS_I18N_LOCALE"),-1);
		request.setAttribute("minEquipment", minEquipment);			
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}
}
